package com.open.launcher.promote;

import android.content.Context;
import android.text.TextUtils;

import com.open.launcher.utils.APPLog;


public class UpgradeHelper {
    private String versionCode;
    private String versionName;
    private String versionInfo;
    private String downloadUrl;
    private static UpgradeHelper helper;

    UpgradeHelper(){}

    public static UpgradeHelper getInstance(){
        if(helper==null){
            helper = new UpgradeHelper();
        }
        return helper;
    }

    public boolean checkVersion(Context context) {
        try {
            if (TextUtils.isEmpty(versionCode)) {
                return false;
            }
            int lastCode = Integer.parseInt(versionCode);
            int currentCode = UpgradeUtil.getVersionCode(context);
            APPLog.printInfo("lastCode:"+lastCode+"currentCode:"+currentCode);
            return currentCode < lastCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public String getVersionCode() {
        int lastCode = Integer.parseInt(versionCode);
        return lastCode / 100 + "." + (lastCode % 100) / 10 + "." + lastCode % 10;
    }

    public String getVersionName() {
        return versionName;
    }

    public String getVersionInfo() {
        return versionInfo;
    }

    public String getDownloadUrl() {
        return downloadUrl;
    }

    public void setVersionCode(String versionCode) {
        this.versionCode = versionCode;
    }

    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    public void setVersionInfo(String versionInfo) {
        this.versionInfo = versionInfo;
    }

    public void setDownloadUrl(String downloadUrl) {
        this.downloadUrl = downloadUrl;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("UpgradeHelper{");
        sb.append("versionCode='").append(versionCode).append('\'');
        sb.append(", versionName='").append(versionName).append('\'');
        sb.append(", versionInfo='").append(versionInfo).append('\'');
        sb.append(", downloadUrl='").append(downloadUrl).append('\'');
        sb.append('}');
        return sb.toString();
    }
}
